//
//  AddressModel.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/31.
//

import SwiftUI

// MARK: - 数据模型
struct AddressModel: Codable {
    let pachist: [AddressPachist]?
}

struct AddressPachist: Codable {
    var selenify: String?
    let granally: String?
    let interestacle: String?
    let otlet: String?
    let gladiproof: [AddressSecond]?
    
    enum CodingKeys: CodingKey {
        case selenify
        case granally
        case interestacle
        case otlet
        case gladiproof
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.selenify = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .selenify) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .selenify) {
                return stringValue
            }
            return nil
        }()
        self.granally = try container.decodeIfPresent(String.self, forKey: .granally)
        self.interestacle = try container.decodeIfPresent(String.self, forKey: .interestacle)
        self.otlet = try container.decodeIfPresent(String.self, forKey: .otlet)
        self.gladiproof = try container.decodeIfPresent([AddressSecond].self, forKey: .gladiproof)
    }
}

// MARK: - Pod层级模型 (第二级)
struct AddressSecond: Codable {
    var selenify: String?
    let granally: String?
    let interestacle: String?
    let gastreur: String?
    let whether: String?
    let gladiproof: [AddressThird]?
    
    enum CodingKeys: CodingKey {
        case selenify
        case granally
        case interestacle
        case gastreur
        case whether
        case gladiproof
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.selenify = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .selenify) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .selenify) {
                return stringValue
            }
            return nil
        }()
        self.granally = try container.decodeIfPresent(String.self, forKey: .granally)
        self.interestacle = try container.decodeIfPresent(String.self, forKey: .interestacle)
        self.gastreur = try container.decodeIfPresent(String.self, forKey: .gastreur)
        self.whether = try container.decodeIfPresent(String.self, forKey: .whether)
        self.gladiproof = try container.decodeIfPresent([AddressThird].self, forKey: .gladiproof)
    }
}

// MARK: - SubPod层级模型 (第三级)
struct AddressThird: Codable {
    var selenify: String?
    let granally: String?
    let interestacle: String?
    let agshootial: String?
    
    enum CodingKeys: CodingKey {
        case selenify
        case granally
        case interestacle
        case agshootial
    }
    
    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.selenify = {
            if let intValue = try? container.decodeIfPresent(Int.self, forKey: .selenify) {
                return String(intValue)
            }
            if let stringValue = try? container.decodeIfPresent(String.self, forKey: .selenify) {
                return stringValue
            }
            return nil
        }()
        self.granally = try container.decodeIfPresent(String.self, forKey: .granally)
        self.interestacle = try container.decodeIfPresent(String.self, forKey: .interestacle)
        self.agshootial = try container.decodeIfPresent(String.self, forKey: .agshootial)
    }
}

// MARK: - 转换为通用数据模型的扩展
extension AddressModel {
    func toDataItems() -> [NDAddressItem] {
        return pachist?.map { $0.toDataItem() } ?? []
    }
}

extension AddressPachist {
    func toDataItem() -> NDAddressItem {
        let children = gladiproof?.map { $0.toDataItem() }
        return NDAddressItem(
            id: granally ?? "",
            name: interestacle ?? "",
            children: children
        )
    }
}

extension AddressSecond {
    func toDataItem() -> NDAddressItem {
        let children = gladiproof?.map { $0.toDataItem() }
        return NDAddressItem(
            id: granally ?? "",
            name: interestacle ?? "",
            children: children
        )
    }
}

extension AddressThird {
    func toDataItem() -> NDAddressItem {
        return NDAddressItem(
            id: granally ?? "",
            name: interestacle ?? "",
            children: nil
        )
    }
}

struct NDAddressItem: Identifiable, Hashable {
    let id: String
    let name: String
    let children: [NDAddressItem]?
    var isLeaf: Bool { children?.isEmpty ?? true }
    
    init(id: String, name: String, children: [NDAddressItem]? = nil) {
        self.id = id
        self.name = name
        self.children = children
    }
}
